package com.wondersgroup.wuhan_demo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import com.wondersgroup.wuhan_demo.base.BaseActivity;
import com.wondersgroup.wuhan_demo.net.MyCallbackBean;
import com.wondersgroup.wuhan_demo.net.OkHttpHelper;
import com.wondersgroup.wuhan_demo.net.Param;
import com.wondersgroup.wuhan_demo.util.BitmapUtil;
import com.wondersgroup.wuhan_demo.util.FileUtil;
import com.wondersgroup.wuhan_demo.util.SPUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.mrobot.core.log.LogUtils;
import okhttp3.Call;
import okhttp3.MediaType;

/**
 * Created by liyaqing on 2018/5/16 0016.
 */

public class MyTestActivity extends BaseActivity {
//        private String orgCode = "";
//    private String userName = "";
//    private String passWord = "";
//    private String orgCode = "31000013";
//    private String userName = "31000013";
//    private String passWord = "1234abcd_";
    private File file;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_test);
        ButterKnife.bind(this);
         boolean analyze=FileUtil.analyzeFile(this);
         if (analyze) {
             getAuthKey(Constant.orgCode, Constant.userName, Constant.passWord);
         }
    }




    private void getAuthKey(final String orgCodeStr, String userName, String passWord) {
        Vector v = new Vector<Param>();
        v.add(new Param("orgCode", orgCodeStr));
        v.add(new Param("userName", userName));
        v.add(new Param("passWord", passWord));
        OkHttpUtils.postString()
                .url(Constant.URL)
                .headers(OkHttpHelper.getHeaders())
                .mediaType(MediaType.parse("text/xml;charset=UTF-8"))
                .content(OkHttpHelper.requestContent(v, "getAuthKey"))
                .build()
                .execute(new MyCallbackBean<Element>() {
                    @Override
                    public void onError(Call call, Exception e) {
                        e.printStackTrace();
//                        toast("获取AuthKey失败！");
                        Log.i("dd", "发卡失败！");
                    }

                    @Override
                    public void onResponse(Element response) {
                        if (response != null) {
                            String message = response.element("message").getText();
                            String resultCode = response.element("resultCode").getText();
                            if ("10000".equals(resultCode)) {
                                String authKey = response.element("authKey").getText();
                                Log.i("authKey", authKey);
//                                syncCardInfo(orgCodeStr, authKey, "5", mIdCardInfo.getCardId());
                                getInfo(orgCodeStr, authKey, "5", "AF4AC400C96D09069E5B9BE793FCA138E5660295F2C44346E8BD7DC0C629CDFC:1");
//                                syncCardInfo(orgCodeStr, authKey, DataUtil.ConverToString(new Date(), "yyyyMMdd"),"411424199208104222", "李亚清", "2");
                            } else {
//                               Log.i("dd","发卡失败！");
                                Toast.makeText(getApplicationContext(), message + "", Toast.LENGTH_SHORT).show();
                            }
                        }

                    }
                });
    }

    private void syncCardInfo(String orgCode, String authKey, String fksj, String sfzhm, String xm, String xb) {
        String v = "    <orgCode>" + orgCode + "</orgCode>\n" +
                "         <authKey>" + authKey + "</authKey>\n" +
                "         <cardInfo>\n" +
                "            <isRealName>0</isRealName>\n" +
                "            <fksj>" + fksj + "</fksj>\n" +
                "            <klx>3</klx>\n" +
                "            <orgCode>" + orgCode + "</orgCode>\n" +
                "            <yycsdm>420101</yycsdm>\n" +
                "            <issuingMode>2</issuingMode>\n" +
                "            <bankCode/>\n" +
                "            <peopleInfo>\n" +
                "               <zjlx>01</zjlx>\n" +
                "               <zjhm>" + sfzhm + "</zjhm>\n" +
                "               <sfzhm>" + sfzhm + "</sfzhm>\n" +
                "               <xm>" + xm + "</xm>\n" +
                "               <xb>" + xb + "</xb>\n" +
                "            </peopleInfo>\n" +
                "         </cardInfo>\n";
        OkHttpUtils.postString()
                .url(Constant.URL)
                .headers(OkHttpHelper.getHeaders())
                .mediaType(MediaType.parse("text/xml;charset=UTF-8"))
                .content(OkHttpHelper.requestContent(v, "syncCardInfo"))
                .build()
                .execute(new MyCallbackBean<Element>() {
                    @Override
                    public void onError(Call call, Exception e) {
                        e.printStackTrace();
                        Log.i("dd", "点击屏幕重试！");
                    }

                    @Override
                    public void onResponse(Element response) {
                        if (response != null) {
                            String message = response.element("message").getText();
                            String resultCode = response.element("resultCode").getText();
                            if ("10000".equals(resultCode)) {
                                try {
                                    Log.i("dd", "发卡成功！请取走您的二维码！");
                                    JSONObject jsonObject = new JSONObject(message);
                                    String bitmapStr = jsonObject.getString("img");
                                    Log.i("bitmapStr", bitmapStr);
                                    Bitmap bitmap = BitmapUtil.base64ToBitmap(bitmapStr);
                                    Log.i("bitmap", bitmap.toString());
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                    Log.i("dd", "发卡失败！点击屏幕重试");
                                }


                            } else {
                                Log.i("dd", "发卡失败！点击屏幕重试");
                                Toast.makeText(getApplicationContext(), message + "", Toast.LENGTH_SHORT).show();
                            }
                        }

                    }
                });
    }

    private void getInfo(String orgCodeStr, String authKey, String type, String numStr) {
        Vector v = new Vector<Param>();
        v.add(new Param("orgCode", orgCodeStr));
        v.add(new Param("authKey", authKey));
        v.add(new Param("type", type));
        v.add(new Param("numStr", numStr));
        OkHttpUtils.postString()
                .url(Constant.URL)
                .headers(OkHttpHelper.getHeaders())
                .mediaType(MediaType.parse("text/xml;charset=UTF-8"))
                .content(OkHttpHelper.requestContent(v, "getInfo"))
                .build()
                .execute(new MyCallbackBean<Element>() {
                    @Override
                    public void onError(Call call, Exception e) {
                        e.printStackTrace();
                        Log.i("dd", "获取个人信息失败！");
//                        setTvTip("获取信息失败！点击屏幕重试");
                    }

                    @Override
                    public void onResponse(Element response) {
                        if (response != null) {
                            String message = response.elementText("message");
                            String resultCode = response.elementText("resultCode");
                            if ("10000".equals(resultCode)) {
                                Element cardInfo = response.element("cardInfo");
                                Element peopleInfo = cardInfo.element("peopleInfo");
                                String kh = cardInfo.elementText("kh");
                                Log.i("kh==", kh);
                                SPUtils.put(MyApplication.getContextObject(), getString(R.string.kh), kh);
                                if (peopleInfo != null) {
//                                    setUserInfoVisible();
                                    String sfzhm = peopleInfo.elementText("sfzhm");
                                    String xm = peopleInfo.elementText("xm");
                                    String xb = peopleInfo.elementText("xb");
                                    if ("1".equals(xb)) {
                                        xb = "男";
                                    } else if ("2".equals(xb)) {
                                        xb = "女";
                                    }

                                    SPUtils.put(MyApplication.getContextObject(), getString(R.string.xm), xm);
                                    SPUtils.put(MyApplication.getContextObject(), getString(R.string.xb), xb);
//                                    tvSfzhm.setText(IdCardUtil.tmIdCard(sfzhm));
//                                    tvXm.setText(xm);
//                                    tvXb.setText(xb);
                                    Log.i("xx==", sfzhm + "," + xm + "," + xb);
//                                  String nl=peopleInfo.elementText("")

                                }
                            } else {
                                Log.i("dd", "获取信息失败！点击屏幕重试");
                            }
                        }

                    }
                });
    }

    @OnClick(R.id.content)
    public void onViewClicked() {
        Log.i("onview", "ddddddd");
    }
}
